
package linuxbox.admin.bean;

import java.util.*;

import linuxbox.BoxStatic;
import linuxbox.exception.*;
import linuxbox.util.*;
import linuxbox.admin.logic.*;
import linuxbox.bean.*;
import linuxbox.admin.exception.*;

public class DeptBean extends TableBean {
	private	int m_iID;
	private	String m_sName;
	private String m_sAddress;
	private String m_sPhone;
	private String m_sEmail;
	private int m_bDisable;
	private String m_sNote;
	
	public DeptBean(DeptKey pk, DbAdapter db) {
		this.init(pk, db);
	}
	
	public DeptBean(DeptKey pk) {
		this.init(pk, null);
	}

	private	void init(DeptKey pk, DbAdapter db) {
		super.init(db);
		if (pk != null)
			m_iID = pk.m_iID;
		super.setTableName("department");
	}

	protected void load() throws DeptBeanException {
		if (!m_bLoaded) {
			try {
				String sql = "select id, name, phone, address, email, note, disable from department where id = " + m_iID;
				makeConnection();
				BoxStatic.logger.debug(sql);
				m_db.executeQuery(sql);
				
				if (m_db.next()) {
					m_sName = m_db.getString("name");
					m_sPhone = m_db.getString("phone");
					m_sAddress = m_db.getString("address");
					m_sEmail = m_db.getString("email");
					m_sNote = m_db.getString("note");
					m_bDisable = m_db.getInt("disable");
				} else {
					close();
					throw new DeptBeanException("Load error:  m_iID not found!");
				}
				m_bLoaded = true;
				close();
			} catch(DbBeanException e1) {
				throw new DeptBeanException("Can't load the record: " + e1.getMessage());
			} catch(TableBeanException e2) {
				throw new DeptBeanException("Can't load the record: " + e2.getMessage());
			}
		}
	}

	public void store() throws DeptBeanException {
		try {
			String where = " id = " + m_iID;
			super.updateTable(where);
		} catch(TableBeanException e2) {
			throw new DeptBeanException("Can't update in store: " + e2.getMessage());
		}
	}
	
	public void refresh() throws DeptBeanException {
		m_bLoaded = false;
		load();
	}

	protected DeptKey create(Department dept) throws DeptBeanException {
		try {
			String sql = "select max(id) from department";
			makeConnection();
			m_db.executeQuery(sql);
			
			if (m_db.next()) {
				int max_id = m_db.getInt(1);
				m_iID = max_id + 1;
				
				String disable = "0";
				if (dept.getDisable())
					disable = "1";
					
				sql = "insert into department(id, name, address, phone, email, note, "
					+ "disable) values(" + m_iID + ", '" + dept.getName() + " ', '"
					+ dept.getAddress() + "', '" + dept.getPhone() + "', '" 
					+ dept.getEmail() + "', '" + dept.getNote() + "', " 
					+ disable + ")";
				BoxStatic.logger.debug(sql);
				m_db.executeUpdate(sql);
			}
			
			close();
			return new DeptKey(m_iID);
		} catch(DbBeanException e1) {
			throw new DeptBeanException("Can't create the Police record: " + e1.getMessage());
		} catch(TableBeanException e2) {
			throw new DeptBeanException("Can't create the Police record: " + e2.getMessage());
		}
	}

	protected void remove() throws DeptBeanException {
		try {
			String sql = "delete department where id = " + m_iID;
			BoxStatic.logger.debug(sql);
			makeConnection();
			m_db.executeUpdate(sql);
			close();
		} catch(DbBeanException e1) {
			throw new DeptBeanException("Can't delete the Police record: " + e1.getMessage());
		} catch(TableBeanException e2) {
			throw new DeptBeanException("Can't delete the Police record: " + e2.getMessage());
		}
	}
	
	protected DeptKey findByKey() throws DeptBeanException {
		DeptKey pk = null;
		try {
			String sql = "select id from department where id = " + m_iID;
			BoxStatic.logger.debug(sql);
			makeConnection();
			m_db.executeQuery(sql);

			if (m_db.next()) {
				pk = new DeptKey(m_db.getInt(1));
			}
			close();
		} catch(DbBeanException e1) {
			throw new DeptBeanException("Can't find the Police record: " + e1.getMessage());
		} catch(TableBeanException e2) {
			throw new DeptBeanException("Can't find the Police record: " + e2.getMessage());
		}
		return pk;
	}

	protected ArrayList findBySql(String sqlWhere) throws DeptBeanException {
		ArrayList v = null;
		try {
			String sql = "select id from department " + sqlWhere;
			BoxStatic.logger.debug(sql);
			makeConnection();
			m_db.executeQuery(sql);
			DeptKey pk = null;
			v = new ArrayList();
			while (m_db.next()) {
				pk = new DeptKey(m_db.getInt(1));
				v.add(pk);
			}
			close();
		} catch(DbBeanException e1) {
			throw new DeptBeanException("Can't find record: " + e1.getMessage());
		} catch(TableBeanException e2) {
			throw new DeptBeanException("Can't find record: " + e2.getMessage());
		}
		return v;
	}

	public int getID() {
		return m_iID;
	}

	public String getName() throws DeptBeanException {
		load();
		return m_sName;
	}
	
	public void setName(String str) throws DeptBeanException {
		m_sName = str;
		setString("name", str);
	}

	public boolean getDisable() throws DeptBeanException {
		load();
		if(m_bDisable == 1)
			return true;
		else
			return false;
	}
	
	public void setDisable(boolean dis) throws DeptBeanException {
		if (dis == true)
			m_bDisable = 1;
		else
			m_bDisable = 0;
			
		setInteger("disable", m_bDisable);
	}

	public String getNote() throws DeptBeanException {
		load();
		return m_sNote;
	}
	
	public void setNote(String str) throws DeptBeanException {
		if (str != null) {
			m_sNote = str;
			setString("note", str);
		}
	}

	public String getAddress() throws DeptBeanException {
		load();
		return m_sAddress;
	}
	
	public void setAddress(String str) throws DeptBeanException {
		if (str != null) {
			m_sAddress = str;
			setString("address", str);
		}
	}

	public String getPhone() throws DeptBeanException {
		load();
		return m_sPhone;
	}
	
	public void setPhone(String str) throws DeptBeanException {
		if (str != null) {
			m_sPhone = str;
			setString("phone", str);
		}
	}

	public String getEmail() throws DeptBeanException {
		load();
		return m_sEmail;
	}
	
	public void setEmail(String str) throws DeptBeanException {
		if (str != null) {
			m_sEmail = str;
			setString("email", str);
		}
	}
}